package com.healthyeating.manage.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.healthyeating.manage.domain.vo.RecipeVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.manage.domain.Recipe;
import com.healthyeating.manage.service.IRecipeService;
import com.healthyeating.common.core.page.TableDataInfo;

/**
 * 食谱管理Controller
 *
 * @author 段奇乐
 * @date 2025-01-19
 */
@RestController
@RequestMapping("/manage/recipe")
public class RecipeController extends BaseController {

    @Autowired
    private IRecipeService recipeService;

    /**
     * 查询食谱管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecipeVo recipeVo) {
        startPage();
        Map<String, Object> map = recipeService.selectRecipeList(recipeVo);
        List<Recipe> oldList = new ArrayList<>();
        List<RecipeVo> newList = new ArrayList<>();
        if (map.get("oldList") != null) {
            oldList = (List<Recipe>) map.get("oldList");
        }

        if (map.get("newList") != null) {
            newList = (List<RecipeVo>) map.get("newList");
        }
//        List<RecipeVo> list = recipeService.selectRecipeList(recipeVo);
        return getDataTable(oldList, newList);
    }

    /**
     * 获取食谱管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:query')")
    @GetMapping(value = "/{recipeId}")
    public AjaxResult getInfo(@PathVariable("recipeId") Long recipeId) {
        return success(recipeService.selectRecipeByRecipeId(recipeId));
    }

    /**
     * 新增食谱管理
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:add')")
    @Log(title = "食谱管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Recipe recipe) {
        return toAjax(recipeService.insertRecipe(recipe));
    }

    /**
     * 修改食谱管理
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:edit')")
    @Log(title = "食谱管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Recipe recipe) {
        return toAjax(recipeService.updateRecipe(recipe));
    }

    /**
     * 删除食谱管理
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:remove')")
    @Log(title = "食谱管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recipeIds}")
    public AjaxResult remove(@PathVariable Long[] recipeIds) {
        return toAjax(recipeService.deleteRecipeByRecipeIds(recipeIds));
    }

    /**
     * 批量修改食谱管理
     */
    @PreAuthorize("@ss.hasPermi('manage:recipe:edit')")
    @Log(title = "食谱管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{recipeIds}")
    public AjaxResult batchEdit(@PathVariable Long[] recipeIds) {
        return toAjax(recipeService.batchUpdateRecipe(recipeIds));
    }
}
